package com.my.reactor.basic;

import lombok.extern.slf4j.Slf4j;
import reactor.core.Disposable;
import reactor.core.publisher.Flux;
import reactor.core.scheduler.Scheduler;
import reactor.core.scheduler.Schedulers;

import java.time.Duration;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author: marshal
 * @date: 2023-07-21 5:10 PM
 */
@Slf4j
public class FluxTest {
    public static void main(String[] args) {
        test3();
    }

    public static void test() {
        Duration period = Duration.of(100L, ChronoUnit.MILLIS);
//        Flux.interval(period).take(10L).subscribe(System.out::println);
    }

    public static void test1() {
        Scheduler single = Schedulers.single();
        Disposable schedule = single.schedule(() -> {
            log.debug("this is run.");
        }, 10000L, TimeUnit.MILLISECONDS);
        Flux<Integer> integerFlux = Flux.fromIterable(List.of(1, 2, 3)).subscribeOn(single);
        integerFlux.subscribe();
    }

    public static void test2() {
        Flux.range(1, 10)
                .subscribeOn(Schedulers.parallel())
                .map(i -> {
                    // Perform some computation or task
                    return i * 2;
                })
                .subscribe(System.out::println);
    }

    public static void test3() {
        Scheduler single = Schedulers.single();
        single.schedule(() -> {
            log.debug("this is run.");
        }, 10000L, TimeUnit.MILLISECONDS);
        Flux.range(1, 10)
                .publishOn(single)
                .subscribe();
    }
}
